package com.atech.ebayadmin.merchandise.service.impl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.atech.ebayadmin.common.ConstConfig;
import com.atech.ebayadmin.common.MdConstConfig;
import com.atech.ebayadmin.common.util.Uuid;
import com.atech.ebayadmin.merchandise.dao.IBatchDAO;
import com.atech.ebayadmin.merchandise.dao.IMerchandiseDAO;
import com.atech.ebayadmin.merchandise.model.Record;
import com.atech.ebayadmin.merchandise.service.IBatchService;
import com.atech.ebayadmin.merchandise.service.IMerchandiseService;
import com.atech.ebayadmin.merchandise.vo.RecordVO;
import com.atech.ebayadmin.system.vo.UserVO;

public class BatchService implements IBatchService{
	private final static org.apache.commons.logging.Log logger = 
		org.apache.commons.logging.LogFactory.getLog(BatchService.class);
    private IMerchandiseService mdService;
    private IBatchDAO BatchDAO;
    private IMerchandiseDAO merchandiseDAO;
    
    //Added by Doug for info collection. - Sep23, 2012
    int numOfNew = 0;
	int numOfUpdated = 0;
	
	@Override
	public int getNumOfNew(){
		return numOfNew;
	}
	
	@Override
	public int getNumOfUpdated(){
		return numOfUpdated;
	}
	//Doug's code ends here!

	public IBatchDAO getBatchDAO() {
		return BatchDAO;
	}
	public void setBatchDAO(IBatchDAO BatchDAO) {
		this.BatchDAO = BatchDAO;
	}
	
	@Override
	public String importFile(UserVO user, String filePath) throws Exception {

		logger.debug("[BatchService].importFile.in");
		logger.debug("filePath = " + filePath);

		File sourceFile = new File(filePath.trim());
		
		List sequenceNumList = new ArrayList();
		
		if(!sourceFile.exists()){
			throw new RuntimeException("请重新选择文件！");
		}
		
		numOfNew = 0;
		numOfUpdated = 0;

		FileInputStream fis = null;
		BufferedInputStream bis = null;
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		
		try {
			fis = new FileInputStream(sourceFile);
			bis = new BufferedInputStream(fis);
			
			Workbook sourceBook = Workbook.getWorkbook(bis);
			
			Sheet sheet = sourceBook.getSheet(0);
			boolean entireRowNotEmpty = true;
			
			
			
			//start from the 2nd row
			int rowIndex = 1;
			
			RecordVO importFields = new RecordVO();
			Cell[] firstRow = sheet.getRow(0);
			int titleCol = -1;
			for (Cell titleCell : firstRow){
				titleCol ++;
				String titleContent = titleCell.getContents();
				
				if("序号".equals(titleContent)){
					importFields.setSequenceNum(String.valueOf(titleCol));
					continue;
				}
				if("资产号".equals(titleContent)){
					importFields.setAssetsNum(String.valueOf(titleCol));
					continue;
				}
				if("自编号".equals(titleContent)){
					importFields.setSelfNum(String.valueOf(titleCol));
					continue;
				}
				if("公司名称".equals(titleContent)){
					importFields.setCompName(String.valueOf(titleCol));
					continue;
				}
				if("业务类型".equals(titleContent)){
					importFields.setBusType(String.valueOf(titleCol));
					continue;
				}
				if("项目/工程号".equals(titleContent)){
					importFields.setProjNum(String.valueOf(titleCol));
					continue;
				}
				if("项目/工程名称".equals(titleContent)){
					importFields.setProjName(String.valueOf(titleCol));
					continue;
				}
				if("资产类型".equals(titleContent)){
					importFields.setProdType(String.valueOf(titleCol));
					continue;
				}
				if("资产分类".equals(titleContent)){
					importFields.setProdClass(String.valueOf(titleCol));
					continue;
				}
				if("设备分类".equals(titleContent)){
					importFields.setEquipClass(String.valueOf(titleCol));
					continue;
				}
				if("设备类型".equals(titleContent)){
					importFields.setEquipType(String.valueOf(titleCol));
					continue;
				}
				if("设备名称".equals(titleContent)){
					importFields.setEquipName(String.valueOf(titleCol));
					continue;
				}
				if("设备别名".equals(titleContent)){
					importFields.setEquipNickName(String.valueOf(titleCol));
					continue;
				}
				if("光缆类型".equals(titleContent)){
					importFields.setFiberType(String.valueOf(titleCol));
					continue;
				}
				if("光缆芯数".equals(titleContent)){
					importFields.setFiberNum(String.valueOf(titleCol));
					continue;
				}
				if("光缆长度".equals(titleContent)){
					importFields.setFiberLength(String.valueOf(titleCol));
					continue;
				}
				if("厂家名".equals(titleContent)){
					importFields.setFactoryName(String.valueOf(titleCol));
					continue;
				}
				if("供货商".equals(titleContent)){
					importFields.setProvider(String.valueOf(titleCol));
					continue;
				}
				if("设备型号".equals(titleContent)){
					importFields.setEquipModel(String.valueOf(titleCol));
					continue;
				}
				if("S/N".equals(titleContent)){
					importFields.setSn(String.valueOf(titleCol));
					continue;
				}
				if("MAC".equals(titleContent)){
					importFields.setMacAddr(String.valueOf(titleCol));
					continue;
				}
				if("模块号".equals(titleContent)){
					importFields.setModelNum(String.valueOf(titleCol));
					continue;
				}
				if("规格".equals(titleContent)){
					importFields.setStandard(String.valueOf(titleCol));
					continue;
				}
				if("单价".equals(titleContent)){
					importFields.setUnitPrice(String.valueOf(titleCol));
					continue;
				}
				if("入库日期".equals(titleContent)){
					importFields.setInputDate(String.valueOf(titleCol));
					continue;
				}
				if("出库日期".equals(titleContent)){
					importFields.setManDate(String.valueOf(titleCol));
					continue;
				}
				if("仓库管理人".equals(titleContent)){
					importFields.setWareHouseMan(String.valueOf(titleCol));
					continue;
				}
				if("使用日期".equals(titleContent)){
					importFields.setUseDate(String.valueOf(titleCol));
					continue;
				}
				if("负责人".equals(titleContent)){
					importFields.setChargeMan(String.valueOf(titleCol));
					continue;
				}
				if("资产管理人".equals(titleContent)){
					importFields.setAssetsMgnt(String.valueOf(titleCol));
					continue;
				}
				if("设备所在省".equals(titleContent)){
					importFields.setEquipLocalProvice(String.valueOf(titleCol));
					continue;
				}
				if("设备所在市".equals(titleContent)){
					importFields.setEquipLocalCity(String.valueOf(titleCol));
					continue;
				}
				if("设备所在区".equals(titleContent)){
					importFields.setEquipLocalDistrict(String.valueOf(titleCol));
					continue;
				}
				if("设备所在村镇".equals(titleContent)){
					importFields.setEquipLocalTown(String.valueOf(titleCol));
					continue;
				}
				if("光缆起点".equals(titleContent)){
					importFields.setFiberStart(String.valueOf(titleCol));
					continue;
				}
				if("光缆终点".equals(titleContent)){
					importFields.setFiberEnd(String.valueOf(titleCol));
					continue;
				}
				if("光缆起始码号".equals(titleContent)){
					importFields.setFiberStartCode(String.valueOf(titleCol));
					continue;
				}
				if("光缆终止码号".equals(titleContent)){
					importFields.setFiberEndCode(String.valueOf(titleCol));
					continue;
				}
				if("具体位置".equals(titleContent)){
					importFields.setExactLocation(String.valueOf(titleCol));
					continue;
				}
				if("所属机架".equals(titleContent)){
					importFields.setBelongFrame(String.valueOf(titleCol));
					continue;
				}
				if("所在机框".equals(titleContent)){
					importFields.setLocalFrame(String.valueOf(titleCol));
					continue;
				}
				if("所在槽道".equals(titleContent)){
					importFields.setLocalChannel(String.valueOf(titleCol));
					continue;
				}
				if("当前状态".equals(titleContent)){
					importFields.setCurrStatus(String.valueOf(titleCol));
					continue;
				}
//				if("IP 地址".equals(titleContent)){
//					importFields.set(String.valueOf(titleCol));
//					continue;
//				}
				if("设备功能".equals(titleContent)){
					importFields.setEquipFunction(String.valueOf(titleCol));
					continue;
				}
				if("修改日期".equals(titleContent)){
					importFields.setModifyDate(String.valueOf(titleCol));
					continue;
				}
				if("修改原因".equals(titleContent)){
					importFields.setModifyResult(String.valueOf(titleCol));
					continue;
				}
				if("备注".equals(titleContent)){
					importFields.setRemarks(String.valueOf(titleCol));
					continue;
				}
				
				throw new Exception("不能识别的列，列名为："+titleContent+"。请确保列名正确后重试！");
			}
			

			System.out.println(" The title information is: "+importFields);
			do{
				/*System.out.println("**********Doug*********: Now we're in row #"+rowIndex);*/
				
				if(rowIndex == sheet.getRows()){
					//This indicates the end of the table
					break;
				}
				
				Record record = null;
				int cellIndex = 0;
				boolean isNew = true;				
				Cell[] row = sheet.getRow(rowIndex);
				
				//Added by Doug for issue fix on Sep23
				//"批量导入：当excel中某行自第n列（n非最后一列）后没有数据，则会出现错误"批量载入数据遇到错误！请重试！n"
				//（假设该行最后一个数据为"光缆类型"，则出现错误"批量载入数据遇到错误！请重试！13"）"
				for (Cell fieldCell : row){
					
				}
				
				int numOfColumns = 0;
				if (null != row){
					numOfColumns = row.length;					
				}else{
					handleUnmatchCase("表格为空！");
				}
				
				//Revised by Doug for issue fix on Sep23
				String sequenceNum= null;
				int pos = -1;
				if (StringUtils.isNotBlank(importFields.getSequenceNum())){
					pos = Integer.parseInt(importFields.getSequenceNum());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						sequenceNum = row[pos].getContents();
						if(StringUtils.isNotBlank(sequenceNum)){
							System.out.println("old record, sequenceNum = " + sequenceNum);
							record = merchandiseDAO.getRecord(new Integer(sequenceNum));
							
							if(null == record){
								throw new Exception("序号为：" + sequenceNum + " 的资产并不存在于系统中。请修改为正确的序号；或者清除其序号，使之作为新资产添加入系统。");
							}
							
							//if(sequenceNumList.contains(sequenceNumList)){
							if(sequenceNumList.contains(sequenceNum)){
								throw new Exception("有多个序号为：" + sequenceNum + " 的资产。请检查批量载入的文件信息。请注意，在此行之前的数据已经被导入数据库中。"+
										"如果其中有新增数据项，为使导入能够成功，请在再次尝试导入时将其删除，或者为其添加序号。");
							}
							
							sequenceNumList.add(sequenceNum);
							System.out.println("old record before being updated = " + sequenceNum);
							isNew = false;
							//record.cleanData();
							
						} else {
							record = new Record();
						}
					}
				}

				pos = -1;
				String assetsNum= null;
				if (StringUtils.isNotBlank(importFields.getAssetsNum())){
					pos = Integer.parseInt(importFields.getAssetsNum());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						//Doug: As requested by customer on Dec14, assetsNum should not be revised through import
						
						/* Old:*/
						/*
						 * Dec-16,2012 Doug
						 * Requirement reverted.
						 */
						assetsNum = row[pos].getContents();
						record.setAssetsNum(assetsNum);
						
						//New:
						/*String newAssetsNum = row[pos].getContents();
						assetsNum = newAssetsNum;
						String oldAssetsNum = record.getAssetsNum();
						
						if(newAssetsNum.equals(oldAssetsNum)){
							record.setAssetsNum(assetsNum);
						}else{
							throw new Exception("资产号不一致！导入表中序列号"+sequenceNum+"的资产资产号为" + newAssetsNum + "。数据库中同一序列号的资产其资产号为" + oldAssetsNum);
						}*/
						
						
					} else {
						record.setAssetsNum("");
					}
				}
				

				// if self num is not empty, then update the existing record
				// else add a new record
				pos = -1;
				String selfNum = null;
				if (StringUtils.isNotBlank(importFields.getSelfNum())){
					pos = Integer.parseInt(importFields.getSelfNum());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						selfNum = row[pos].getContents();
						record.setSelfNum(selfNum);
					} else {
						record.setSelfNum(null);
					}
				}
				
				if(StringUtils.isBlank(sequenceNum) && StringUtils.isBlank(assetsNum) && StringUtils.isBlank(selfNum)){
					//Doug: we're using null sequenceNum, null assetsNum & selfNum to indicate the end of a table
					//otherwise we won't know the end of the table
					entireRowNotEmpty = false;
					break;
					//Wrong! --> Doug: here should be 'continue'? sequenceNum, assetsNum & selfNum are null means that row is null, but NOT the end of the table, rite?
					/*rowIndex ++;
					continue;*/
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getCompName())){
					pos = Integer.parseInt(importFields.getCompName());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setCompName(row[pos].getContents());
					} else {
						record.setCompName(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getBusType())){
					pos = Integer.parseInt(importFields.getBusType());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setBusType(row[pos].getContents());
					} else {
						record.setBusType(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getProjNum())){
					pos = Integer.parseInt(importFields.getProjNum());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setProjNum(row[pos].getContents());
					} else {
						record.setProjNum(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getProjName())){
					pos = Integer.parseInt(importFields.getProjName());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setProjName(row[pos].getContents());
					} else {
						record.setProjName(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getProdType())){
					pos = Integer.parseInt(importFields.getProdType());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						String prodTypeValue = row[pos].getContents();
						String prodTypeKey = "";
						if(StringUtils.isNotBlank(prodTypeValue)){
							prodTypeKey = getKeyByValue(MdConstConfig.prodType, prodTypeValue.trim());
							
							if(StringUtils.isBlank(prodTypeKey)){
								handleUnmatchCase("资产类型无法匹配："+prodTypeValue);
							}
						}
		            	System.out.println("prodTypeValue = " +prodTypeValue+ ",prodTypeKey = " + prodTypeKey);
						record.setProdType(prodTypeKey);
					} else {
						record.setProdType(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getProdClass())){
					pos = Integer.parseInt(importFields.getProdClass());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						String prodClassValue = row[pos].getContents();
						String prodClassKey = "";
						if(StringUtils.isNotBlank(prodClassValue)){
							prodClassKey = getKeyByValue(MdConstConfig.prodClass, prodClassValue.trim());
							
							if(StringUtils.isBlank(prodClassKey)){
								handleUnmatchCase("资产分类无法匹配："+prodClassValue);
							}
						}
		            	System.out.println("prodClassValue = " +prodClassValue+ ", prodClassKey= " + prodClassKey);
						record.setProdClass(prodClassKey);
					} else {
						record.setProdClass(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipClass())){
					pos = Integer.parseInt(importFields.getEquipClass());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipClass(row[pos].getContents());
					} else {
						record.setEquipClass(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipType())){
					pos = Integer.parseInt(importFields.getEquipType());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipType(row[pos].getContents());
					} else {
						record.setEquipType(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipName())){
					pos = Integer.parseInt(importFields.getEquipName());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipName(row[pos].getContents());
					} else {
						record.setEquipName(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipNickName())){
					pos = Integer.parseInt(importFields.getEquipNickName());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipNickName(row[pos].getContents());
					} else {
						record.setEquipNickName(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getFiberType())){
					pos = Integer.parseInt(importFields.getFiberType());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						String fiberTypeValue = row[pos].getContents();
						String fiberTypeKey = "";
						if(StringUtils.isNotBlank(fiberTypeValue)){
							fiberTypeKey = getKeyByValue(MdConstConfig.fiberType, fiberTypeValue.trim());
							
							if(StringUtils.isBlank(fiberTypeKey)){
								handleUnmatchCase("光缆类型无法匹配："+fiberTypeValue);
							}
						}
		            	System.out.println("fiberTypeValue = " +fiberTypeValue+ ",fiberTypeKey = " + fiberTypeKey);
						record.setFiberType(fiberTypeKey);
					} else {
						record.setFiberType(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getFiberNum())){
					pos = Integer.parseInt(importFields.getFiberNum());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						String fiberNumImpt = row[pos].getContents();
		            	System.out.println("fiberNumImpt = " +fiberNumImpt);
						/*if(StringUtils.isNotBlank() && StringUtils.isNumeric(fiberNumImpt)){*/
		            	/*
		            	 * Dec-19-2012 by Doug
		            	 * isNumeric() here always returns false. isNumeric() can NOT be used for negative int
		            	 * */
		            	if(StringUtils.isNotBlank(fiberNumImpt)){
							
							/*
							 * Dec-19, 2012 by Doug
							 * As requested by customer, fiberNum should be checked during import & make sure it's not negative							 * 
							 * */
							//record.setFiberNum(Integer.parseInt(fiberNumImpt));
							if((new BigDecimal(fiberNumImpt)).signum() == -1){ //or if(Integer.parseInt(fiberNumImpt) > 0)
								handleUnmatchCase("光缆芯数不能为负数："+(Integer.parseInt(fiberNumImpt)));
							}else{
							record.setFiberNum(Integer.parseInt(fiberNumImpt));
							}
							
						} else {
							record.setFiberNum(null);
						}
					} else {
						record.setFiberNum(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getFiberLength())){
					pos = Integer.parseInt(importFields.getFiberLength());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						
						String fiberLengthImpt = row[pos].getContents();
						if(StringUtils.isNotBlank(fiberLengthImpt)){
							
							/*
							 * Dec-19, 2012 by Doug
							 * As requested by customer, fiberLength should be checked during import & make sure it's not negative							 * 
							 * */
							//record.setFiberLength(Integer.parseInt(fiberLengthImpt));
							if((new BigDecimal(fiberLengthImpt)).signum() == -1){ //or if(Integer.parseInt(fiberLengthImpt) > 0)
								handleUnmatchCase("光缆长度不能为负数："+(Integer.parseInt(fiberLengthImpt)));
							}else{
							record.setFiberLength(Integer.parseInt(fiberLengthImpt));
							}
							
						}  else {
							record.setFiberLength(null);
						}
						
					} else {
						record.setFiberLength(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getFactoryName())){
					pos = Integer.parseInt(importFields.getFactoryName());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setFactoryName(row[pos].getContents());
					} else {
						record.setFactoryName(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getProvider())){
					pos = Integer.parseInt(importFields.getProvider());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setProvider(row[pos].getContents());
					} else {
						record.setProvider(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipModel())){
					pos = Integer.parseInt(importFields.getEquipModel());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipModel(row[pos].getContents());
					} else {
						record.setEquipModel(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getSn())){
					pos = Integer.parseInt(importFields.getSn());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						//Doug: As requested by customer on Dec14, S/N should not be revised through import
						
						/* Old:*/
						/*
						 * Dec-16,2012 Doug
						 * Requirement reverted.
						 */
						record.setSn(row[pos].getContents());
						
						//New:
						/*String newSN = row[pos].getContents();
						String oldSN = record.getSn();
						
						if(newSN.equals(oldSN)){
							record.setSn(newSN);
						}else{
							throw new Exception("S/N号不一致！导入表中序列号"+sequenceNum+"的资产S/N号为" + newSN + "。数据库中同一序列号的资产其S/N号为" + oldSN);
						}*/
						
					} else {
						record.setSn(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getMacAddr())){
					pos = Integer.parseInt(importFields.getMacAddr());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setMacAddr(row[pos].getContents());
					} else {
						record.setMacAddr(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getModelNum())){
					pos = Integer.parseInt(importFields.getModelNum());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setModelNum(row[pos].getContents());
					} else {
						record.setModelNum(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getStandard())){
					pos = Integer.parseInt(importFields.getStandard());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setStandard(row[pos].getContents());
					} else {
						record.setStandard(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getUnitPrice())){
					pos = Integer.parseInt(importFields.getUnitPrice());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						String imptFiledTmp = row[pos].getContents();
						
						if(StringUtils.isNotBlank(imptFiledTmp)){
							//Added by Doug on Dec11 since we should not have negative UnitPrice
							if((new BigDecimal(imptFiledTmp)).signum() == -1){
								handleUnmatchCase("单价不能为负数："+(new BigDecimal(imptFiledTmp)));
							}else{
							record.setUnitPrice(new BigDecimal(imptFiledTmp));
							}
						}  else {
							record.setUnitPrice(null);
						}
					} else {
						record.setUnitPrice(null);
					}
				}
								
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getInputDate())){
					pos = Integer.parseInt(importFields.getInputDate());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						String cellContent = row[pos].getContents();
						if(StringUtils.isNotBlank(cellContent)){
							try{
								validatDate(cellContent);
								record.setInputDate(dateFormat.parse(cellContent));
							} catch (ParseException e) {
								throw new Exception("第"+rowIndex+"行入库日期格式错误,正确格式为年月日(例如：20120228),错误数据为:"+row[pos].getContents());
							}
						}
					}
					/*if(isNew && null == record.getInputDate()){
						record.setInputDate(new Date(System.currentTimeMillis()));
					}*/
					/*
					 * Doug@2013-01-04
					 * As requested by customer, for inputDate we should ALWAYS make use of system time if it's a new record
					 * */
					if(isNew){
						record.setInputDate(new Date(System.currentTimeMillis()));
					}
				}
								
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getManDate())){
					pos = Integer.parseInt(importFields.getManDate());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						if(StringUtils.isNotBlank(row[pos].getContents())){
							try{
								validatDate(row[pos].getContents());
								record.setManDate(dateFormat.parse(row[pos].getContents()));
							} catch (ParseException e) {
								throw new Exception("第"+rowIndex+"行出库日期格式错误,正确格式为年月日(例如：20120228),错误数据为:"+row[pos].getContents());
							}
						} else {
							record.setManDate(null);
						}
					} else {
						record.setManDate(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getWareHouseMan())){
					pos = Integer.parseInt(importFields.getWareHouseMan());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setWareHouseMan(row[pos].getContents());
					} else {
						record.setWareHouseMan(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getUseDate())){
					pos = Integer.parseInt(importFields.getUseDate());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						if(StringUtils.isNotBlank(row[pos].getContents())){
							try{
								validatDate(row[pos].getContents());
								record.setUseDate(dateFormat.parse(row[pos].getContents()));
							} catch (ParseException e) {
								throw new Exception("第"+rowIndex+"行使用日期格式错误,正确格式为年月日(例如：20120228),错误数据为:"+row[pos].getContents());
							}
						} else {
							record.setUseDate(null);
						}
					} else {
						record.setUseDate(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getChargeMan())){
					pos = Integer.parseInt(importFields.getChargeMan());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setChargeMan(row[pos].getContents());
					} else {
						record.setChargeMan(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getAssetsMgnt())){
					pos = Integer.parseInt(importFields.getAssetsMgnt());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setAssetsMgnt(row[pos].getContents());
					} else {
						record.setAssetsMgnt(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipLocalProvice())){
					pos = Integer.parseInt(importFields.getEquipLocalProvice());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipLocalProvice(row[pos].getContents());
					} else {
						record.setEquipLocalProvice(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipLocalCity())){
					pos = Integer.parseInt(importFields.getEquipLocalCity());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipLocalCity(row[pos].getContents());
					} else {
						record.setEquipLocalCity(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipLocalDistrict())){
					pos = Integer.parseInt(importFields.getEquipLocalDistrict());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipLocalDistrict(row[pos].getContents());
					} else {
						record.setEquipLocalDistrict(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipLocalTown())){
					pos = Integer.parseInt(importFields.getEquipLocalTown());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipLocalTown(row[pos].getContents());
					} else {
						record.setEquipLocalTown(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getFiberStart())){
					pos = Integer.parseInt(importFields.getFiberStart());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setFiberStart(row[pos].getContents());
					} else {
						record.setFiberStart(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getFiberEnd())){
					pos = Integer.parseInt(importFields.getFiberEnd());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setFiberEnd(row[pos].getContents());
					} else {
						record.setFiberEnd(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getFiberStartCode())){
					pos = Integer.parseInt(importFields.getFiberStartCode());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setFiberStartCode(row[pos].getContents());
					} else {
						record.setFiberStartCode(null);
					}
				}

				pos = -1;
				if (StringUtils.isNotBlank(importFields.getFiberEndCode())){
					pos = Integer.parseInt(importFields.getFiberEndCode());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setFiberEndCode(row[pos].getContents());
					} else {
						record.setFiberEndCode(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getExactLocation())){
					pos = Integer.parseInt(importFields.getExactLocation());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setExactLocation(row[pos].getContents());
					} else {
						record.setExactLocation(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getBelongFrame())){
					pos = Integer.parseInt(importFields.getBelongFrame());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setBelongFrame(row[pos].getContents());
					} else {
						record.setBelongFrame(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getLocalFrame())){
					pos = Integer.parseInt(importFields.getLocalFrame());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setLocalFrame(row[pos].getContents());
					} else {
						record.setLocalFrame(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getLocalChannel())){
					pos = Integer.parseInt(importFields.getLocalChannel());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setLocalChannel(row[pos].getContents());
					} else {
						record.setLocalChannel(null);
					}
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getCurrStatus())){
					pos = Integer.parseInt(importFields.getCurrStatus());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						String currStatusValue = row[pos].getContents();
						String currStatusKey = "";
						if(StringUtils.isNotBlank(currStatusValue)){
							currStatusKey = getKeyByValue(MdConstConfig.currStatus, currStatusValue.trim());
							
							if(StringUtils.isBlank(currStatusKey)){
								handleUnmatchCase("当前状态无法匹配："+currStatusValue);
							}
						} else {
							throw new Exception("第"+rowIndex+"行当前状态不能为空！");
						}
		            	System.out.println("currStatusValue = " +currStatusValue+ ",currStatusKey = " + currStatusKey);
						record.setCurrStatus(currStatusKey);
					} else {
						record.setCurrStatus(null);
					}
				}
				

				pos = -1;
				if (StringUtils.isNotBlank(importFields.getEquipFunction())){
					pos = Integer.parseInt(importFields.getEquipFunction());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setEquipFunction(row[pos].getContents());
					} else {
						record.setEquipFunction(null);
					}
				}
				
				record.setModifyDate(new Date(System.currentTimeMillis()));
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getModifyResult())){
					pos = Integer.parseInt(importFields.getModifyResult());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setModifyResult(row[pos].getContents());
					}
				}
				
				if(StringUtils.isBlank(record.getModifyResult())){
					record.setModifyResult("批量导入数据");
				}
				
				pos = -1;
				if (StringUtils.isNotBlank(importFields.getRemarks())){
					pos = Integer.parseInt(importFields.getRemarks());
				}
				
				if (pos > -1){
					if(pos < numOfColumns){
						record.setRemarks(row[pos].getContents());
					} else {
						record.setRemarks(null);
					}
				}
				
				System.out.println("isNew = " + isNew);
				System.out.println("row = " + rowIndex +", record = " + record);

				if(isNew){
					merchandiseDAO.saveRecord(record);
					mdService.saveModHisty(user, record);
					numOfNew++;
				} else {
					merchandiseDAO.updateRecord(record);
					mdService.saveModHisty(user, record);
					numOfUpdated++;
				}
				
				rowIndex ++;
			} while (entireRowNotEmpty);
			
			sourceBook.close();
        } catch (Exception e) {
            throw e;
        } finally {
        	
			try {
				fis.close();
			} catch (IOException e) {
				logger.error(e.getMessage(), e);
			}
			fis = null;
		
			try {
				bis.close();
			} catch (IOException e) {
				logger.error(e.getMessage(), e);
			}
			bis = null;
			
			housekeep();
        }
        
		return null;
	}
	@Override
	public String exportFile(Map filter, RecordVO dispRe) throws Exception {
		OutputStream output = null;
		FileInputStream fis = null;

		System.out.println("[BatchService].exportFile in ");
		System.out.println("[BatchService].filter = " + filter);
		
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			
			List<Record> totalRecordList = merchandiseDAO.listRecord(filter, null);

			System.out.println("totalRecordList = " + totalRecordList);
			System.out.println("totalRecordList.size = " + totalRecordList.size());
			
			String fileSaveaddr = createExportFile(totalRecordList, dispRe);
			
			response.reset();
			byte[] fileNameByte = ("ExportFile" + ".xls").getBytes("GBK");
			String fileName = new String(fileNameByte, "8859_1");
			response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
			output = response.getOutputStream();
			fis = new FileInputStream(fileSaveaddr);
			byte[] b = new byte[1024];
			int i = 0;
			while ((i = fis.read(b)) > 0) {
				output.write(b, 0, i);
			}
			output.flush();
			
		}catch (Exception e) {
            throw e;
		}finally{
			if(null != fis){
				fis.close();
			}
			if(null != output){
				output.close();
			}
		}
		
		return null;
	}
	
	private String createExportFile(List<Record> list, RecordVO dispRe) throws Exception{
		String path=ConstConfig.contextPath + ConstConfig.excelPath+"/"+Uuid.create().toString32()+".xls";
		String sequenceNum = null;
		String assetsNum = null;
		String selfNum = null;
		String fieldName = null;
		try {
	            //打开文件
                WritableWorkbook book = null;
                book = Workbook.createWorkbook(new File(path));
//			    WritableWorkbook book = Workbook.createWorkbook(new File("d:\test.xsl"));\
	            //参数0表示这是第一页
	            WritableSheet sheet = book.createSheet("资产信息", 0);
	            int titleIndex = 0;
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "序号"));

	            if("1".equals(dispRe.getAssetsNum())){
	            	sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "资产号"));
	            }
	            if("1".equals(dispRe.getSelfNum())){
	            	sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "自编号"));
	            }
	            if("1".equals(dispRe.getCompName())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "公司名称"));
	            }
	            if("1".equals(dispRe.getBusType())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "业务类型"));
	            }
	            if("1".equals(dispRe.getProjNum())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "项目/工程号"));
	            }
	            if("1".equals(dispRe.getProjName())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "项目/工程名称"));
	            }
	            if("1".equals(dispRe.getProdType())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "资产类型"));
	            }
	            if("1".equals(dispRe.getProdClass())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "资产分类"));
	            }
	            if("1".equals(dispRe.getEquipClass())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备分类"));
	            }
	            if("1".equals(dispRe.getEquipType())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备类型"));
	            }
	            if("1".equals(dispRe.getEquipName())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备名称"));
	            }
	            if("1".equals(dispRe.getEquipNickName())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备别名"));
	            }
	            if("1".equals(dispRe.getFiberType())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "光缆类型"));
	            }
	            if("1".equals(dispRe.getFiberNum())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "光缆芯数"));
	            }
	            if("1".equals(dispRe.getFiberLength())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "光缆长度"));
	            }
	            if("1".equals(dispRe.getFactoryName())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "厂家名"));
	            }
	            if("1".equals(dispRe.getProvider())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "供货商"));
	            }
	            if("1".equals(dispRe.getEquipModel())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备型号"));
	            }
	            if("1".equals(dispRe.getSn())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "S/N"));
	            }
	            
	            //Added by Doug for MAC on Sep21
	            if("1".equals(dispRe.getMacAddr())){
	            	sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "MAC"));
	            }
	            
	            if("1".equals(dispRe.getModelNum())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "模块号"));
	            }
	            if("1".equals(dispRe.getStandard())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "规格"));
	            }
	            if("1".equals(dispRe.getUnitPrice())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "单价"));
	            }
	            if("1".equals(dispRe.getInputDate())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "入库日期"));
	            }
	            if("1".equals(dispRe.getManDate())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "出库日期"));
	            }
	            if("1".equals(dispRe.getWareHouseMan())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "仓库管理人"));
	            }
	            if("1".equals(dispRe.getUseDate())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "使用日期"));
	            }
	            if("1".equals(dispRe.getChargeMan())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "负责人"));
	            }
	            if("1".equals(dispRe.getAssetsMgnt())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "资产管理人"));
	            }
	            if("1".equals(dispRe.getEquipLocalProvice())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备所在省"));
	            }
	            if("1".equals(dispRe.getEquipLocalCity())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备所在市"));
	            }
	            if("1".equals(dispRe.getEquipLocalDistrict())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备所在区"));
	            }
	            if("1".equals(dispRe.getEquipLocalTown())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备所在村镇"));
	            }
	            if("1".equals(dispRe.getFiberStart())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "光缆起点"));
	            }
	            if("1".equals(dispRe.getFiberEnd())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "光缆终点"));
	            }
	            if("1".equals(dispRe.getFiberStartCode())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "光缆起始码号"));
	            }
	            if("1".equals(dispRe.getFiberEndCode())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "光缆终止码号"));
	            }
	            if("1".equals(dispRe.getExactLocation())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "具体位置"));
	            }
	            if("1".equals(dispRe.getBelongFrame())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "所属机架"));
	            }
	            if("1".equals(dispRe.getLocalFrame())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "所在机框"));
	            }
	            if("1".equals(dispRe.getLocalChannel())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "所在槽道"));
	            }
	            if("1".equals(dispRe.getCurrStatus())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "当前状态"));
	            }
//	            if("1".equals(dispRe.getIp())){
	            
	            //Commented by Doug since IP is no longer needed
	            /*sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "IP 地址"));*/
	            
//	            }
	            if("1".equals(dispRe.getEquipFunction())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "设备功能"));
	            }
	            if("1".equals(dispRe.getModifyDate())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "修改日期"));
	            }
	            if("1".equals(dispRe.getModifyResult())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "修改原因"));
	            }
	            if("1".equals(dispRe.getRemarks())){
	            sheet.addCell(new jxl.write.Label(titleIndex ++, 0, "备注"));
	            }
	            
	            int rowIndex = 1;

	    		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
	    		DecimalFormat numFormat = new DecimalFormat("###");
	    		DecimalFormat decFormat = new DecimalFormat("0.00");
	            
	            for(Record record : list){
	            	int colIndex = 0;
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, numFormat.format(record.getSequenceNum().intValue())));
	            	sequenceNum = String.valueOf(record.getSequenceNum());
	            	
	            	// 'if' statement added by Doug on Dec10
	            	if("1".equals(dispRe.getAssetsNum())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getAssetsNum()));
	            	assetsNum = String.valueOf(record.getAssetsNum());
	            	}
	            	
		            if("1".equals(dispRe.getSelfNum())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getSelfNum()));
	            	selfNum = record.getSelfNum();
		            }
		            if("1".equals(dispRe.getCompName())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getCompName()));
		            }
		            if("1".equals(dispRe.getBusType())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getBusType()));
		            }
		            if("1".equals(dispRe.getProjNum())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getProjNum()));
		            }
		            if("1".equals(dispRe.getProjName())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getProjName()));
		            }
		            if("1".equals(dispRe.getProdType())){
	            	try{
	            		String prodTypeStr = record.getProdType();
	            		System.out.println("record.getProdType() = " +prodTypeStr);
	            		Object prodType = null;
	            		if(StringUtils.isNotBlank(prodTypeStr)){
	            			prodType = MdConstConfig.prodType.get(record.getProdType());
	            		}
		            	System.out.println("prodType = " + prodType);
		            	prodType = null != prodType? prodType:"";
		            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, (String)prodType));
	            	} catch (Exception e){
	            		fieldName = "资产类型";
	            		throw e;
	            	}
		            }
		            if("1".equals(dispRe.getProdClass())){
	            	try{
	            		String prodClassStr = record.getProdClass();
		            	System.out.println("record.getProdClass() = " +prodClassStr);
		            	Object prodClass = null;
		            	if(StringUtils.isNotBlank(prodClassStr)){
		            		prodClass = MdConstConfig.prodClass.get(record.getProdClass());
		            	}
		            	System.out.println("prodClass = " +prodClass );
		            	prodClass = null != prodClass? prodClass:"";
		            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, (String)prodClass));
	            	} catch (Exception e){
	            		fieldName = "资产分类";
	            		throw e;
	            	}
		            }
		            if("1".equals(dispRe.getEquipClass())){
	            	try{
	            		//Revised by Doug on Oct19 to remove the dropdown of equipClass
		            	/*System.out.println("record.getEquipClass() = " +record.getEquipClass());
		            	Object equipClass = MdConstConfig.equipClass.get(record.getEquipClass());
		            	System.out.println("equipClass = " + equipClass);
		            	equipClass = null != equipClass? equipClass:"";
		            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, (String)equipClass));*/
	            		
	            		System.out.println("record.getEquipClass() = " +record.getEquipClass());
		            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipClass()));
	            	} catch (Exception e){
	            		fieldName = "设备分类";
	            		throw e;
	            	}
		            }
		            if("1".equals(dispRe.getEquipType())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipType()));
		            }
		            if("1".equals(dispRe.getEquipName())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipName()));
		            }
		            if("1".equals(dispRe.getEquipNickName())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipNickName()));
		            }
		            if("1".equals(dispRe.getFiberType())){
	            	try{
	            		String fiberTypeStr = record.getFiberType();
	            		System.out.println("record.getFiberType() = " +fiberTypeStr);
	            		Object fiberType = null;
	            		if(StringUtils.isNotBlank(fiberTypeStr)){
			            	fiberType = MdConstConfig.fiberType.get(record.getFiberType());
	            		}
		            	System.out.println("fiberType = " + fiberType);
		            	fiberType = null != fiberType? fiberType:"";
		            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, (String)fiberType));
	            	} catch (Exception e){
	            		fieldName = "光缆类型";
	            		throw e;
	            	}
		            }
		            if("1".equals(dispRe.getFiberNum())){
		            	if(null != record.getFiberNum()){
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, numFormat.format(record.getFiberNum().intValue())));
		            	} else {
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, ""));
		            	}

		            }
		            if("1".equals(dispRe.getFiberLength())){
		            	if(null != record.getFiberLength()){
			            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, numFormat.format(record.getFiberLength().intValue())));
		            	} else {
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, ""));
		            	}

		            }
		            if("1".equals(dispRe.getFactoryName())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getFactoryName()));
		            }
		            if("1".equals(dispRe.getProvider())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getProvider()));
		            }
		            if("1".equals(dispRe.getEquipModel())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipModel()));
		            }
		            if("1".equals(dispRe.getSn())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getSn()));
		            }
	            	
	            	//Added by Doug for MAC on Sep21
		            if("1".equals(dispRe.getMacAddr())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getMacAddr()));
		            }
	            	
		            if("1".equals(dispRe.getModelNum())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getModelNum()));
		            }
		            if("1".equals(dispRe.getStandard())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getStandard()));

		            }
		            if("1".equals(dispRe.getUnitPrice())){
		            	if(null != record.getUnitPrice()){
			            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, decFormat.format(record.getUnitPrice())));
		            	} else {
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, ""));
		            	}

		            }
		            if("1".equals(dispRe.getInputDate())){
		            	if(null != record.getInputDate()){
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, dateFormat.format(record.getInputDate())));
		            	} else {
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, ""));
		            	}

		            }
		            if("1".equals(dispRe.getManDate())){
		            	if(null != record.getManDate()){
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, dateFormat.format(record.getManDate())));
		            	} else {
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, ""));
		            	}
		            }
		            if("1".equals(dispRe.getWareHouseMan())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getWareHouseMan()));

		            }
		            if("1".equals(dispRe.getUseDate())){
		            	if(null != record.getUseDate()){
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, dateFormat.format(record.getUseDate())));
		            	} else {
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, ""));
		            	}

		            }
		            if("1".equals(dispRe.getChargeMan())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getChargeMan()));
		            }
		            if("1".equals(dispRe.getAssetsMgnt())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getAssetsMgnt()));
		            }
		            if("1".equals(dispRe.getEquipLocalProvice())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipLocalProvice()));
		            }
		            if("1".equals(dispRe.getEquipLocalCity())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipLocalCity()));
		            }
		            if("1".equals(dispRe.getEquipLocalDistrict())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipLocalDistrict()));
		            }
		            if("1".equals(dispRe.getEquipLocalTown())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipLocalTown()));
		            }
		            if("1".equals(dispRe.getFiberStart())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getFiberStart()));
		            }
		            if("1".equals(dispRe.getFiberEnd())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getFiberEnd()));
		            }
		            if("1".equals(dispRe.getFiberStartCode())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getFiberStartCode()));
		            }
		            if("1".equals(dispRe.getFiberEndCode())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getFiberEndCode()));
		            }
		            if("1".equals(dispRe.getExactLocation())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getExactLocation()));
		            }
		            if("1".equals(dispRe.getBelongFrame())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getBelongFrame()));
		            }
		            if("1".equals(dispRe.getLocalFrame())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getLocalFrame()));
		            }
		            if("1".equals(dispRe.getLocalChannel())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getLocalChannel()));
		            }
		            if("1".equals(dispRe.getCurrStatus())){
	            	try{
	            		String currentStatus = record.getCurrStatus();
	            		System.out.println("record.getCurrStatus() = " +currentStatus);
	            		Object currStatus = null;
	            		if(StringUtils.isNotBlank(currentStatus)){
	            			currStatus = MdConstConfig.currStatus.get(record.getCurrStatus());
	            		}
		            	System.out.println("currStatus = " + currStatus);
		            	currStatus = null != currStatus? currStatus:"";
		            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, (String)currStatus));
	            	} catch (Exception e){
	            		fieldName = "当前状态";
	            		throw e;
	            	}
		            }
//		            if("1".equals(dispRe.getIp())){
	            	
	            	//Commented by Doug on Sep21 coz IP is no longer needed
	            	/*sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getIp()));*/
	            	
//		            }
		            if("1".equals(dispRe.getEquipFunction())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getEquipFunction()));
		            }
		            if("1".equals(dispRe.getModifyDate())){
		            	if(null != record.getModifyDate()){
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, dateFormat.format(record.getModifyDate())));
		            	} else {
		            		sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, ""));
		            	}

		            }
		            if("1".equals(dispRe.getModifyResult())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getModifyResult()));
		            }
		            if("1".equals(dispRe.getRemarks())){
	            	sheet.addCell(new jxl.write.Label(colIndex++, rowIndex, record.getRemarks()));
		            }

	            	rowIndex ++;
                }
	            //写入数据并关闭文件
	            book.write();
	            book.close();
        } catch (Exception e) {
        	String msg = "";
        	if(StringUtils.isNotBlank(sequenceNum)){
        		msg = "序号为：" + sequenceNum;
        	}
        	if(StringUtils.isNotBlank(assetsNum)){
        		msg = "资产号为：" + assetsNum;
        	}
        	if(StringUtils.isNotBlank(selfNum)){
        		if(StringUtils.isNotBlank(msg)){
        			msg = msg + ", ";
        		}
        		msg = msg + "自编号为：" + selfNum;
        	}
        	
        	if(StringUtils.isNotBlank(fieldName)){
        		if(StringUtils.isNotBlank(msg)){
        			msg = msg + ", ";
        		}
        		msg = msg + "属性(错误数据)：" + fieldName;
        	}
        	if(StringUtils.isBlank(msg)){
        		throw e;
        	} else {
            	e.printStackTrace();
        		throw new RuntimeException(msg, e);
        	}
        }
		housekeep();
        
        return path;
    }
	
	private String getKeyByValue(Map entries, String value){
		
		if( null == entries|| !entries.containsValue(value) || null == value){
			return "";
		}
		Iterator keys = entries.keySet().iterator();
		
		while(keys.hasNext()){
			String key = (String)keys.next();
			if(value.equals(entries.get(key))){
				return key;
			}
		}
		
		return "";
	}
	
	private void housekeep(){
		String filePath = ConstConfig.contextPath + ConstConfig.excelPath;
		File tempFolder = new File(filePath);
		
		for (File tempFile : tempFolder.listFiles()) {
			tempFile.deleteOnExit();
		}
		
	}
	
	private void handleUnmatchCase(String message){
		throw new RuntimeException(message);
	}
	
	public IMerchandiseDAO getMerchandiseDAO() {
		return merchandiseDAO;
	}
	public void setMerchandiseDAO(IMerchandiseDAO merchandiseDAO) {
		this.merchandiseDAO = merchandiseDAO;
	}

	public IMerchandiseService getMdService() {
		return mdService;
	}

	public void setMdService(IMerchandiseService mdService) {
		this.mdService = mdService;
	}
	
	private int parseInt(String integerStr){
		
		if(StringUtils.isNotBlank(integerStr) && StringUtils.isNumeric(integerStr)){
			return Integer.parseInt(integerStr);
		} else {
			return 0;
		}
	}
	
	private void validatDate(String dateStr) throws Exception{
		if(null == dateStr){
			return;
		}
		
		if(dateStr.length()>8){
			throw new Exception("日期位数超过8位！正确格式为年月日(例如：20120228),错误数据为:"+dateStr);
		}
	}
	
}
	
